package com.zhen.controller;

import com.zhen.domain.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@Controller
public class UserController {

    private static final Log LOGGER = LogFactory.getLog(UserController.class);

    @RequestMapping("/{formName}")
    public String loginForm(@PathVariable String formName, Model model){
        User user = new User();
        model.addAttribute(user);
        return formName;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@Valid @ModelAttribute User user, Errors errors,Model model){
        LOGGER.info(user);
        if(errors.hasErrors()){
            return "registerForm";
        }
        model.addAttribute(user);
        return "success";
    }

}
